package com.itheima.comtroller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/Member")
public class MemberController {
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private MemberService memberService;
    @RequestMapping("/sendLogin")
    public Result sendLogin(String telephone){
        //调用工具类发送验证码
        Integer code = ValidateCodeUtils.generateValidateCode(6);
        try {
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,code.toString());
            jedisPool.getResource().setex(RedisMessageConstant.SENDTYPE_LOGIN+telephone,6*50,code.toString());
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }
    @RequestMapping("/login")
    public Result login(HttpServletResponse response, @RequestBody Map m){
        String validateCode = (String)m.get("validateCode");
        String telephone = (String)m.get("telephone");
        String s = jedisPool.getResource().get(RedisMessageConstant.SENDTYPE_LOGIN + telephone);
            if(validateCode!=null&&s!=null&&validateCode.equals(s)){
               //验证码输入正确之后，判断当前用户是否为会员
                Member member = memberService.findVipByPhone(telephone);
                if(member==null){
                   //不是会员则进行自动注册,要进行主键返回
                    member = new Member();
                    member.setRegTime(new Date());
                    member.setPhoneNumber(telephone);
                    memberService.registerVip(member);
                }
                //将对象转为json的String格式
                String string = JSON.toJSON(member).toString();
                //将用户信息存储到redis进行保存 有效期为30天
                jedisPool.getResource().setex(telephone,60*60*24*30,string);
                //存到cookie
                Cookie cookie = new Cookie("LOGIN_MEMBER", telephone);
                cookie.setPath("/");
                cookie.setMaxAge(60*60*24*30);
                response.addCookie(cookie);
                return  new Result(true,MessageConstant.LOGIN_SUCCESS);
            }else{
                //验证码校验失败
           return new Result(false,MessageConstant.VALIDATECODE_ERROR);
        }

    }
}
